package com.novunda.platform.web;

import com.novunda.platform.common.exception.StatisticsException;
import com.novunda.platform.common.web.controller.BaseController;
import com.novunda.platform.entities.Statistics;
import com.novunda.platform.entities.StatisticsTime;
import com.novunda.platform.services.StatisticsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;


/**
 * 采集统计Controller
 *
 * @author Lizy
 * @since 2016/9/8
 */
@Controller
@RequestMapping("/conf/statistics")
public class StatisticsController extends BaseController {
    /**
     * 数据错误
     */
    public static final String ERROR_DATA = "数据格式错误!";
    /**
     * 保存成功
     */
    public static final String SAVE_SUCCEED = "保存成功!";
    /**
     * 时间段不能为空
     */
    public static final String ERROR_TIME_EMPTY = "时间段不能为空!";
    @Autowired
    private StatisticsService statisticsService;

    @ModelAttribute("statistics")
    @RequiresPermissions("sys:statistics:view")
    public Statistics get(@RequestParam(required = false) Long id) {
        if (id != null) {
            return statisticsService.getStatistics();
        } else {
            return new Statistics();
        }
    }

    @RequestMapping("")
    @RequiresPermissions("sys:statistics:view")
    public String getStatistics(Model model) {
        model.addAttribute("statistics", statisticsService.getStatistics());
        return "modules/conf/statistics";
    }

    @RequestMapping("default")
    @RequiresPermissions("sys:statistics:view")
    public String getDefaultStatistics(Model model) {
        model.addAttribute("statistics", statisticsService.getDefaultStatistics());
        return "modules/conf/statistics";
    }

    @RequestMapping(value = "save", method = RequestMethod.GET)
    @RequiresPermissions("sys:statistics:edit")
    public String getSave(Model model) {
        return getStatistics(model);
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @RequiresPermissions("sys:statistics:edit")
    public String saveStatistics(Statistics statistics, Model model, RedirectAttributes redirectAttributes) {
        // 检查数据格式是否正确
        if (!beanValidator(model, statistics)) {
            addMessage(redirectAttributes, ERROR_DATA);
            model.addAttribute("message", ERROR_DATA);
            return "modules/conf/statistics";
        }
        List<StatisticsTime> statisticsTimes = statistics.getStatisticsTimes();
        // 检查配置时间段是否为空
        if (null != statisticsTimes && !statisticsTimes.isEmpty()) {
            for (StatisticsTime statisticsTime : statisticsTimes) {
                // 检查数据格式是否正确
                if (!beanValidator(model, statisticsTime)) {
                    addMessage(redirectAttributes, ERROR_DATA);
                    model.addAttribute("message", ERROR_DATA);
                    return "modules/conf/statistics";
                }
            }
            try {
                statisticsService.saveStatistics(statistics);
                addMessage(redirectAttributes, SAVE_SUCCEED);
                model.addAttribute("message", SAVE_SUCCEED);
                return "redirect:" + "/conf/statistics";
            } catch (StatisticsException e) {
                LOG.error(e.getMessage());
                addMessage(redirectAttributes, e.getMessage());
                model.addAttribute("message", e.getMessage());
            }
        } else {
            addMessage(redirectAttributes, ERROR_TIME_EMPTY);
            model.addAttribute("message", ERROR_TIME_EMPTY);
        }
        model.addAttribute("statistics", statistics);
        return "modules/conf/statistics";
    }

}
